/**
 * Copyright (c) 2013, Redsolution LTD. All rights reserved.
 * 
 * This file is part of Xabber project; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License, Version 3.
 * 
 * Xabber is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License,
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
package com.digitalbuana.smiles.data.extension.broadcast;

import java.util.Iterator;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.ServiceDiscoveryManager;

import android.media.AudioManager;
import android.net.Uri;

import com.digitalbuana.smiles.R;
import com.digitalbuana.smiles.data.Application;
import com.digitalbuana.smiles.data.NetworkException;
import com.digitalbuana.smiles.data.OnLoadListener;
import com.digitalbuana.smiles.data.SettingsManager;
import com.digitalbuana.smiles.data.account.AccountItem;
import com.digitalbuana.smiles.data.account.AccountManager;
import com.digitalbuana.smiles.data.connection.ConnectionItem;
import com.digitalbuana.smiles.data.connection.ConnectionManager;
import com.digitalbuana.smiles.data.connection.ConnectionThread;
import com.digitalbuana.smiles.data.connection.OnPacketListener;
import com.digitalbuana.smiles.data.extension.capability.CapabilitiesManager;
import com.digitalbuana.smiles.data.extension.capability.ClientInfo;
import com.digitalbuana.smiles.data.message.AbstractChat;
import com.digitalbuana.smiles.data.message.ChatAction;
import com.digitalbuana.smiles.data.message.MessageManager;
import com.digitalbuana.smiles.data.message.RegularChat;
import com.digitalbuana.smiles.data.notification.EntityNotificationProvider;
import com.digitalbuana.smiles.data.notification.NotificationManager;
import com.digitalbuana.smiles.data.roster.PresenceManager;
import com.digitalbuana.smiles.data.roster.ResourceItem;
import com.digitalbuana.smiles.xmpp.address.Jid;
import com.digitalbuana.smiles.xmpp.attention.Attention;

public class BroadcastManager implements OnPacketListener {

	private final static BroadcastManager instance;
	static {
		instance = new BroadcastManager();
		Application.getInstance().addManager(instance);
	}

	public static BroadcastManager getInstance() {
		return instance;
	}

	public BroadcastManager() {
	}

	@Override
	public void onPacket(ConnectionItem connection, String bareAddress,
			Packet packet) {
		if (!(connection instanceof AccountItem))
			return;
		if (!(packet instanceof Message))
			return;
		if (!SettingsManager.chatsAttention())
			return;
		final String account = ((AccountItem) connection).getAccount();
		if (bareAddress == null)
			return;
		for (PacketExtension packetExtension : packet.getExtensions())
			if (packetExtension instanceof Attention) {
				MessageManager.getInstance().openChat(account, bareAddress);
				MessageManager.getInstance().getOrCreateChat(account, bareAddress).newAction(null, null, ChatAction.attention_requested);
//				attentionRequestProvider.add(new AttentionRequest(account,bareAddress), true);
			}
	}

	public void sendBroadcast(String account, String user, String messageBroadcast) throws NetworkException {
		AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, user);
		if (!(chat instanceof RegularChat))
			throw new NetworkException(R.string.ENTRY_IS_NOT_FOUND);
		String to = chat.getTo();
		if (Jid.getResource(to) == null || "".equals(Jid.getResource(to))) {
			ResourceItem resourceItem = PresenceManager.getInstance().getResourceItem(account, user);
			if (resourceItem == null)
				throw new NetworkException(R.string.NOT_CONNECTED);
			to = resourceItem.getUser(user);
		}
		Message message = new Message();
		message.setTo(to);
		message.setBody(messageBroadcast);
		message.setType(Message.Type.broadcast);
		ConnectionManager.getInstance().sendPacket(account, message);
		chat.newAction(null, null, ChatAction.broadcast_message);
	}

}
